using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.IO;
using System.Xml.Xsl;
using System.Xml;
using System.Reflection;
using Spike.ContractBuilder;

namespace Spike.TestHarness
{
    public partial class TestCaseControl : UserControl
    {
        public TestCaseControl(string filename)
           : base()
        {
            InitializeComponent();
        }

        public TestCaseControl()
        {
            InitializeComponent();
        }

        

        TestCase _case = new TestCase();
        public TestCase Case
        {
            get 
            {
                _case.Input = cntrlInput.Content;
                _case.URL = txtUrl.Text.ToString();
                return _case; 
            }
            set 
            { 
                _case = value;

                cntrlInput.Content = _case.Input;
                txtAssembly.Text = _case.Assembly;
                tip.SetToolTip(txtAssembly, _case.Assembly);                
                txtContract.Text = _case.ContractName;
                tip.SetToolTip(txtContract, _case.ContractName);
                txtEndpoint.Text = _case.EndpointName;
                tip.SetToolTip(txtEndpoint, _case.EndpointName);
                txtMethod.Text = _case.Method;
                tip.SetToolTip(txtMethod, _case.Method);

                txtUrl.Text = _case.URL;

                RefreshLastExecuted();

                tcResults.TabPages.Clear();

                _case.OnUpdated += new EventHandler(_case_OnUpdated);
            }
        }

        /// <summary>
        /// Indicates if the test case input can be deserialized
        /// </summary>
        public bool IsValidTestCase
        {
            get { return errorProvider1.GetError(lblUrl).Length==0;}
        }

        private void RefreshLastExecuted()
        {
            if (_case.LastExecuted.HasValue)
                txtLastExecuted.Text = _case.LastExecuted.Value.ToLongTimeString();
            else
                txtLastExecuted.Text = "Never";
        }

        void _case_OnUpdated(object sender, EventArgs e)
        {
            RefreshLastExecuted();
        }

        public void AddTestCaseResult(TestCaseResult result)
        {
            TestCaseResultControl cntrl = new TestCaseResultControl();
            cntrl.TestName = _case.Name;
            cntrl.Result = result;
            cntrl.Dock = DockStyle.Fill;

            // add the new control to the page and then to the tabbed control
            TabPage page = new TabPage(result.Status.ToString());
            page.Controls.Add(cntrl);            
            page.BackColor = cntrl.BackColor;
            
            tcResults.TabPages.Insert(0, page);
            tcResults.SelectedIndex = 0;

            this.Refresh();
        }

        private void mnuClose_Click(object sender, EventArgs e)
        {
            tcResults.TabPages.Remove(tcResults.SelectedTab);
        }

        private void mnuCloseAll_Click(object sender, EventArgs e)
        {
            for (int i = tcResults.TabPages.Count - 1; i >= 0; i--)
            {
                if (tcResults.TabPages[i] != tcResults.SelectedTab)
                    tcResults.TabPages.RemoveAt(i);
            }
        }
    }
}
